﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Autofac;
using Autofac.Integration.Mvc;
using Autofac.Integration.WebApi;
using eCommerce.App_Start;
using eCommerce.Core;
using eCommerce.Core.Data;
using eCommerce.Data;
using eCommerce.Services.Authentication;
using eCommerce.Services.Brands;
using eCommerce.Services.Categorys;
using eCommerce.Services.Customers;
using eCommerce.Services.Installations;
using eCommerce.Services.Loggings;
using eCommerce.Services.Messages;
using eCommerce.Services.Orders;
using eCommerce.Services.Payments;
using eCommerce.Services.Pictures;
using eCommerce.Services.Products;
using eCommerce.Services.Security;
using eCommerce.Services.Shippings;
using eCommerce.Services.ShoppingCarts;
using eCommerce.Services.Tradings;

namespace eCommerce
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {

            var container = BuildContianer();
            EngineContext.Initialize(false, container);

            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AutoMapperConfig.Config();
        }

        private IContainer BuildContianer()
        {
            var builder = new ContainerBuilder();
            // Configure the container  
            //builder.RegisterHubs(Assembly.GetCallingAssembly());

            //db
            builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)).InstancePerHttpRequest();
            builder.Register<DbContext>(c => new YiObjectContext()).InstancePerHttpRequest();


            //service
            builder.RegisterType<WebWorkContext>().As<IWorkContext>().InstancePerHttpRequest();
            builder.RegisterType<ProductService>().As<IProductService>().InstancePerHttpRequest();
            builder.RegisterType<MessageService>().As<IMessageService>().InstancePerHttpRequest();
            builder.RegisterType<InstallService>().As<IInstallService>().InstancePerHttpRequest();
            builder.RegisterType<PictureService>().As<IPictureService>().InstancePerHttpRequest();
            builder.RegisterType<FormsAuthenticationService>().As<IAuthenticationService>().InstancePerHttpRequest();
            builder.RegisterType<CustomerService>().As<ICustomerService>().InstancePerHttpRequest();
            builder.RegisterType<EncryptionService>().As<IEncryptionService>().InstancePerHttpRequest();
            builder.RegisterType<PermissionService>().As<IPermissionService>().InstancePerHttpRequest();
            builder.RegisterType<CustomerRegistrationService>().As<ICustomerRegistrationService>().InstancePerHttpRequest();
            builder.RegisterType<CategoryService>().As<ICategoryService>().InstancePerHttpRequest();
            builder.RegisterType<BrandService>().As<IBrandService>().InstancePerHttpRequest();
            builder.RegisterType<ItemService>().As<IItemService>().InstancePerHttpRequest();
            builder.RegisterType<ShoppingCartService>().As<IShoppingCartService>().InstancePerHttpRequest();
            builder.RegisterType<PaymentService>().As<IPaymentService>().InstancePerHttpRequest();
            builder.RegisterType<OrderProcessingService>().As<IOrderProcessingService>().InstancePerHttpRequest();
            builder.RegisterType<Logger>().As<ILogger>().InstancePerHttpRequest();
            builder.RegisterType<OrderService>().As<IOrderService>().InstancePerHttpRequest();
            builder.RegisterType<TradingService>().As<ITradingService>().InstancePerHttpRequest();
            builder.RegisterType<ShippingService>().As<IShippingService>().InstancePerHttpRequest();
            builder.RegisterType<OrderTotalCalculationService>().As<IOrderTotalCalculationService>().InstancePerHttpRequest();

            builder.RegisterType<AlipayPaymentMethod>().As<IPaymentMethod>().Named<IPaymentMethod>("AlipayPaymentMethod");
            builder.RegisterType<DummyPaymentMethod>().As<IPaymentMethod>().Named<IPaymentMethod>("DummyPaymentMethod");


            //controller
            var callingAssembly = Assembly.GetCallingAssembly();
            builder.RegisterControllers(callingAssembly);
            builder.RegisterApiControllers(callingAssembly);

            var container = builder.Build();
            //var resolver = new AutofacWebApiDependencyResolver(container);
            var resolver = new AutofacDependencyResolver(container);
            DependencyResolver.SetResolver(resolver);
            GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

            return container;
        }
    }
}
